import {Injectable} from '@angular/core';
import {HttpClientService} from './http-client.service';
import {SKey, StorageService} from './storage.service';
import {Router} from '@angular/router';
import {PubSubService} from '../pub-sub/pub-sub.service';
import {Observable} from 'rxjs/Observable';
import {ConfirmConfig, Func, IPDialogConfig, IPDialogData, LoginUser} from '../model/share.model';
import {MenuClickEvent, MsgChangeEvent} from '../pub-sub/pub-sub.event';
import {ConfirmationService} from 'primeng/api';
import {AppConfig} from '../../dashboard/app/app.config';
import {ObjectCopy} from '../utils';

@Injectable()
export class AppContextService {
  redirectUrl: string;
  printData: boolean;
  currComp: any;
  public msg: MsgChangeEvent;   // 全局消息
  public menuClick: MenuClickEvent;  // 菜单点击事件
  public confirmConfig: ConfirmConfig;  // 确认框信息
  constructor(
    public http: HttpClientService,
    public session: StorageService,
    public router: Router,
    public confirmService: ConfirmationService,
    private pubsub: PubSubService,
  ) {
    this.msg = this.pubsub.msgChange;
    this.menuClick = this.pubsub.menuClick;
    this.confirmConfig = AppConfig.confirmDefault;
    this.printData = true;
  }


  login(userName, userPwd): Observable<any> {
    const url = '/api/Token';
    const model = new LoginUser(userName, userPwd);
    return this.http.post(url, model)
      .do(result => {
        if (result.success) {
          this.session.modules = result.data.modules;
          this.session.user = result.data.user;
          this.session.token = result.data.token;
          this.printLoginData();
          result.message = '登录成功！';
        }
        return result;
      });
  }
  printLoginData() {
    if (this.printData) {
      console.log('modules', this.session.modules);

      // Object.keys(this.session).forEach(field => {
      //   console.log(field, this.session.getObject(field));
      // });
    }
  }
  // showDialog(data) {
  //   this.currComp = data;
  // }
  showDialog(config: IPDialogConfig, service , comp,  data?: IPDialogData) {
    config.display = true;
    this.currComp = {
      service: service,
      component: comp,
      inputs: {
        dialog: {
          config: config,
          data: { inData: data, outData: {} }
        }
      }
    };
  }
  public logout() {
    this.confirm(ObjectCopy(
      AppConfig.confirmLogout,
      {
        yes: () => {
        this.redirectUrl = '/';
        this.session.clear();
        this.msg.info('注销成功！');
        this.router.navigate(['/login']);
        }
      }
    ));
  }
  confirm(config: ConfirmConfig ) {
    ObjectCopy(this.confirmConfig, config);
    this.confirmService.confirm({
      message: this.confirmConfig.msg,
      header: this.confirmConfig.title,
      icon: this.confirmConfig.icon,
      accept: this.confirmConfig.yes,
      reject: this.confirmConfig.no,
      acceptVisible: this.confirmConfig.showYes,
      rejectVisible: this.confirmConfig.showNo
    });
  }
  hasLogin(url: string): boolean {
    // 存在token，返回true;反之则路由导航停止
    if (this.session.has(SKey[SKey.TokenData])) {
      // 保存返回url
      this.redirectUrl = url;
      return true;
    } else {
      this.router.navigate(['/login']);
      return false;
    }
  }
  btnCodeToBtnObject(code: string) {
    if (typeof code === 'string') {
      return { code: code, functionCode: code };
    }
    return code;
  }
}
